package com.dobbinsoft.iotechn.device.config;

import com.dobbinsoft.iotechn.device.mqtt.*;
import com.dobbinsoft.iotechn.device.mqtt.handler.IotMqttMessageHandler;
import com.dobbinsoft.iotechn.device.mqtt.serializer.IotMqttDeserializer;
import com.dobbinsoft.iotechn.device.mqtt.serializer.IotMqttJsonDeserializer;
import com.dobbinsoft.iotechn.device.mqtt.serializer.IotMqttJsonSerializer;
import com.dobbinsoft.iotechn.device.mqtt.serializer.IotMqttSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Description:
 * User: rize
 * Date: 2020/6/22
 * Time: 14:40
 */
@Configuration
public class IotMqttConfig {

    /**
     * @param iotMqttConnectMessageHandlerList 所有 IotMqttMessageHandler 类型的IoC中的对象列表
     * @return
     */
    @Bean
    public Map<Integer, IotMqttMessageHandler> messageHandleRouter(List<IotMqttMessageHandler> iotMqttConnectMessageHandlerList) {
        return iotMqttConnectMessageHandlerList.stream().collect(Collectors.toMap(IotMqttMessageHandler::getCode, v -> v));
    }

    @Bean
    public IotMqttSerializer iotMqttSerializer() {
        return new IotMqttJsonSerializer();
    }

    @Bean
    public IotMqttDeserializer iotMqttDeserializer() {
        return new IotMqttJsonDeserializer();
    }

    @Bean
    public IotMqttClient iotMqttClient() {
        return new IotMqttClient();
    }

}
